package com.evildog.workspace.life.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import org.springframework.util.Assert;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.life.application.dto.MicroHabitPageDTO;
import com.evildog.workspace.life.application.dto.MicroHabitListDTO;
import com.evildog.workspace.life.application.dto.MicroHabitDTO;
import com.evildog.workspace.life.application.bo.MicroHabitBO;
import com.evildog.workspace.life.application.bo.MicroHabitPageBO;
import com.evildog.workspace.life.application.bo.MicroHabitListBO;
import com.evildog.workspace.life.application.convert.MicroHabitConvert;
import java.util.List;
import com.evildog.workspace.life.application.entity.MicroHabit;
import com.evildog.workspace.life.application.entity.MicroHabitPageDO;
import com.evildog.workspace.life.application.entity.MicroHabitListDO;
import com.evildog.workspace.life.application.mapper.MicroHabitMapper;
import com.evildog.workspace.life.application.service.MicroHabitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class MicroHabitServiceImpl extends ServiceImpl<MicroHabitMapper, MicroHabit> implements MicroHabitService {

    @Override
    public PagedResult<MicroHabitPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, MicroHabitPageDTO pageDTO) {
        IPage<MicroHabitPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return MicroHabitConvert.INSTANCE.convert(page);
    }

    @Override
    public List<MicroHabitListBO> getList(List<QueryConditionDTO> queryCondition, MicroHabitListDTO dto) {
        List<MicroHabitListDO> microHabitList = this.baseMapper.getList(queryCondition, dto);
        return MicroHabitConvert.INSTANCE.convert(microHabitList);
    }

    @Override
    public MicroHabitBO getDetailById(Long id) {
        MicroHabit microHabit = getById(id);
        Assert.notNull(microHabit, "microHabit is null!");
        return MicroHabitConvert.INSTANCE.convert(microHabit);
    }

    @Override
    public boolean save(MicroHabitDTO dto){
        MicroHabit newMicroHabit = MicroHabitConvert.INSTANCE.convert(dto);
        return save(newMicroHabit);
    }

    @Override
    public boolean updateById(Long id, MicroHabitDTO dto){
        MicroHabit oldMicroHabit = getById(id);
        Assert.notNull(oldMicroHabit, "microHabit is null!");
        MicroHabit newMicroHabit = MicroHabitConvert.INSTANCE.convert(dto);
        newMicroHabit.setId(id);
        return updateById(newMicroHabit);
    }
}
